#include "Feature.h"
#include <iostream>
Feature::Feature(void)
{
	
}

Feature::~Feature(void)
{
}
void Feature::FastCorner()
{
	FAST(*imagedata, fasecorns, 50, true);
}
void Feature::MserCorner()
{
	MSER _mser = MSER();
	Mat mask = Mat::ones(imagedata->rows, imagedata->cols,CV_8U);
	_mser(*imagedata, msercorns,mask);
}
void Feature::StarCorner()
{
	// the full constructor initialized all the algorithm parameters:
	// maxSize - maximum size of the features. The following
	// values of the parameter are supported:
	// 4, 6, 8, 11, 12, 16, 22, 23, 32, 45, 46, 64, 90, 128
	// responseThreshold - threshold for the approximated laplacian,
	// used to eliminate weak features. The larger it is,
	// the less features will be retrieved
	// lineThresholdProjected - another threshold for the laplacian to
	// eliminate edges
	// lineThresholdBinarized - another threshold for the feature
	// size to eliminate edges.
	// The larger the 2 threshold, the more points you get.
	// StarDetector(int maxSize, int responseThreshold,
	//	int lineThresholdProjected,
	//	int lineThresholdBinarized,
	//	int suppressNonmaxSize);
	

	//  int maxSize CV_DEFAULT(45),
	//	int responseThreshold CV_DEFAULT(30),
	//	int lineThresholdProjected CV_DEFAULT(10),
	//	int lineThresholdBinarized CV_DEFAULT(8),
	//	int suppressNonmaxSize CV_DEFAULT(5))
	int _maxSize = 4;
	int _responseThreshold = 1;
	int _lineThresholdProjected = 100;
	int _lineThresholdBinarized = 80;
	int _suppressNonmaxSize = 5;

	StarDetector _star = StarDetector(_maxSize,_responseThreshold, _lineThresholdProjected,_lineThresholdBinarized, _suppressNonmaxSize);
	_star( *imagedata, starcorns);
}
void Feature::SiftDescriptor()
{
	double _threshold = 0.07;
	double _edgeThreshold = 10;
	
	SIFT::CommonParams _commParams           = SIFT::CommonParams();
	SIFT::DetectorParams _detectorParams     = SIFT::DetectorParams(_threshold, _edgeThreshold);
	SIFT::DescriptorParams _descriptorParams = SIFT::DescriptorParams();
	SIFT _sift = SIFT(_commParams, _detectorParams, _descriptorParams);
	

	std::cout<<"GET_DEFAULT_THRESHOLD"<<std::endl;
	std::cout<<_sift.getDetectorParams().threshold<<std::endl;
	
	std::cout<<"GET_DEFAULT_EDGE_THRESHOLD"<<std::endl;
	std::cout<<_sift.getDetectorParams().edgeThreshold<<std::endl;

	Mat mask = Mat::ones(imagedata->rows, imagedata->cols,CV_8U);
	_sift(*imagedata,mask, _siftfeature.siftcorns, _siftfeature.siftdescriptors, FALSE);
}

void Feature::SurfDescriptor()
{
	SURF _surf;
	_surf = SURF();
	Mat mask = Mat::ones(imagedata->rows, imagedata->cols,CV_8U);
	_surf(*imagedata, mask, _surffeature.surfcorns, _surffeature.descriptors, FALSE);
}
void Feature::DrawFastFeaturePoint(IplImage *img, int start_x, int start_y )
{
	uchar *imgptr = (uchar  *)img->imageData;
	for(int i = 0; i < fasecorns.size(); i ++)
	{
		int x  = (int)(fasecorns[i].pt.x + 0.5);
		int y  = (int)(fasecorns[i].pt.y + 0.5);
		x = start_x  + x;
		y = start_y  + y;
		cvDrawCircle(img, cvPoint(x, y), 1, CV_RGB(255,0,0),1);
	}
}
void Feature::DrawMserFeaturePoint(IplImage *img, int start_x, int start_y)
{
	for(int i = 0;  i <msercorns.size(); i ++)
	{
		CvPoint _p1,_p2;
		std::cout<<msercorns[i].size()<<std::endl;
		for(int j = 0; j < msercorns[i].size() - 1; j ++)
		{
			
			_p1.x = msercorns[i][j].x + start_x;
			_p1.y = msercorns[i][j].y + start_y;

			_p2.x = msercorns[i][j + 1].x + start_x;
			_p2.y = msercorns[i][j + 1].y + start_y;
			cvDrawLine(img, _p1 , _p2 , CV_RGB(255,0,0), 1);
		}
		_p1.x = msercorns[i][j].x + start_x;
		_p1.y = msercorns[i][j].y + start_y;

		_p2.x = msercorns[i][0].x + start_x;
		_p2.y = msercorns[i][0].y + start_y;
		cvDrawLine(img, _p1 , _p2 , CV_RGB(255,0,0), 1);
	}
}
void Feature::DrawStarFeaturePoint(IplImage *img, int start_x, int start_y )
{
	uchar *imgptr = (uchar  *)img->imageData;
	for(int i = 0; i < starcorns.size(); i ++)
	{
		int x  = (int)(starcorns[i].pt.x + 0.5);
		int y  = (int)(starcorns[i].pt.y + 0.5);
		x = start_x  + x;
		y = start_y  + y;
		cvDrawCircle(img, cvPoint(x, y), 1, CV_RGB(255,0,0),1);
	}
}
void Feature::DrawSiftFeaturePoint(IplImage *img, int start_x, int start_y )
{
	uchar *imgptr = (uchar  *)img->imageData;
	for(int i = 0; i < _siftfeature.siftcorns.size(); i ++)
	{
		int x  = (int)(_siftfeature.siftcorns[i].pt.x + 0.5);
		int y  = (int)(_siftfeature.siftcorns[i].pt.y + 0.5);
		x = start_x  + x;
		y = start_y  + y;
		cvDrawCircle(img, cvPoint(x, y), 1, CV_RGB(255,0,0),1);
	}
}
void Feature::DrawSurfFeaturePoint(IplImage *img, int start_x, int start_y )
{
	uchar *imgptr = (uchar  *)img->imageData;
	for(int i = 0; i < _surffeature.surfcorns.size(); i ++)
	{
		int x  = (int)(_surffeature.surfcorns[i].pt.x + 0.5);
		int y  = (int)(_surffeature.surfcorns[i].pt.y + 0.5);
		x = start_x  + x;
		y = start_y  + y;
		cvDrawCircle(img, cvPoint(x, y), 1, CV_RGB(255,0,0),1);
	}
}